/**
 *
 * User: c3t
 * Date: 14-2-17
 * Time: 下午2:33
 */
define(function (require, exports, module) {
    var $ = require('js/lib/jquery');
    require("js/lib/core/jquery.cms.tree");
    var utils = require("js/lib/core/utils");
    var mtree = $("#tree").mytree({
        url: $("#treePath").val(),
        mine: {
            listChild: 0
        },
        callback: {
            onAsyncSuccess: initTree,
            //节点被点击后触发下面函数,得到节点被点击之前的状态
            beforeCheck: function (treeId, treeNode) {
                if (!treeNode.checked) { //treeNode是点击之前checked为false，也就是没被添加的节点，点击后为true
                    //收集在子节点点击之前，状态为false的父节点,这些父节点都要添加
                    var ps = mtree.getCheckParentNodes(treeNode, false);
                    //被点中的节点，也要添加
                    ps.push(treeNode);
                    //添加节点
                    changeGroupChannel(ps, 'add');
                } else { //节点被点击之前checked是true状态。被点击后要被删除。checked变为false
                    var cs = [];
                    //得到被点击的节点的子节点，子节点被点击之前的checked为true,点击后为false ,子节点全部删除
                    mtree.getCheckChildrenNodes(treeNode, true, cs);
                    //被点击节点本身被删除
                    cs.push(treeNode);
                    //删除所有子节点
                    changeGroupChannel(cs, 'del');
                }

            },
            //节点被点击之后checked为flase
            onCheck: function (event, treeId, treeNode) {
                //当节点被点击后，节点的checked为flase，收集他的父节点。如果父节点checked也为flase，就删除掉
                if (!treeNode.checked) {
                    //pn就是要删除的父节点的集合
                    var pn = mtree.getCheckParentNodes(treeNode, false);
                    //删除父节点
                    changeGroupChannel(pn, 'del');
                }
            }
        },
        check: {
            enable: true,
            chkboxType: { "Y": "p", "N": "ps" }
        }
    });


    function changeGroupChannel(ps, flag) {
        var gid = $("#gid").val();

        //得到要处理的节点id数组
        var ns = getObjectToArray(ps);
        //console.info(" ---- "+getArrayToString(ns));
        var nss = utils.getArrayToString(ns);
        if ("add" == flag) {
            $.post("admin/group/addGroupChannel", {"ids": nss, "gid": gid});
        } else if ("del" == flag) {
            $.post("admin/group/delGroupChannel", {"ids": nss, "gid": gid});
        }

    }

    //取出对象数组里的id，存储在change数组里
    function getObjectToArray(as) {
        var change = [];
        for (var i in as) {

            var p = as[i];
            if (p.id > 0) {
                change.push(p.id);
            }
        }
        return change;
    }


    /* //得到某个节点的父节点
     function getParent(treeNode, checked) {
     var ps = [];
     var pn;
     while ((pn = treeNode.getParentNode())) {
     if (pn.checked == checked) {
     ps.push(pn);
     }
     treeNode = pn;
     }
     return ps;
     }

     //得到某个节点的所有子节点
     function getChildren(treeNode, checked, cs) {
     var ccs;
     if (ccs = treeNode.children) {
     for (var i = 0; i < ccs.length; i++) {
     var c = ccs[i];
     if (c.checked == checked) {
     cs.push(c);
     }
     getChildren(c, checked, cs);
     }

     }
     }*/

    function initTree() {
        mtree.expandAll(true);

        //初始化栏目树。并将数据库中存储的栏目选中
        var cids = $("input[name='cids']");
        for (var i = 0; i < cids.length; i++) {
            var cid = cids[i].value;
            var n = mtree.getNodeByParam("id", cid, null);
            mtree.checkNode(n, true, true);
        }
    }
});
